package com.pzl.arcgis;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.TextSymbol;

import java.util.ArrayList;
import java.util.List;

/**
 * ( http://www.gpsspg.com/maps.htm )
 */
public class SymbolsActivity extends AppCompatActivity {

    private MapView mMapView;

    private final SpatialReference wgs84 = SpatialReference.create(4326);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_symbols);

        mMapView = (MapView) findViewById(R.id.mapView);
        ArcGISMap map = new ArcGISMap(Basemap.Type.OCEANS, 28.1800729998, 113.0160140991, 11);
        mMapView.setMap(map);


        //添加一个新的图层
        GraphicsOverlay graphicsOverlay = addGraphicsOverlay(mMapView);
        //add some buoy positions to the graphics overlay
        addPoints(graphicsOverlay);
        addLine(graphicsOverlay);
        addPolygon(graphicsOverlay);
        addText(graphicsOverlay);
    }

    /**
     * 添加一个新的图层
     *
     * @param mapView
     * @return 被添加的新的图层
     */
    private GraphicsOverlay addGraphicsOverlay(MapView mapView) {
        //创建图形图层
        GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
        //把图层加入到mapview
        mapView.getGraphicsOverlays().add(graphicsOverlay);
        return graphicsOverlay;
    }

    /**
     * @param graphicOverlay
     */
    private void addPoints(GraphicsOverlay graphicOverlay) {
        List<Point> pointList = new ArrayList<>();
        //define the buoy locations
        pointList.add(new Point(113.163105, 28.332621, wgs84));
        pointList.add(new Point(112.900943, 28.331603, wgs84));
        pointList.add(new Point(112.85725, 28.204346, wgs84));
        pointList.add(new Point(112.890595, 28.130976, wgs84));
        pointList.add(new Point(112.944637, 28.096312, wgs84));
        pointList.add(new Point(112.99063, 28.089173, wgs84));
        pointList.add(new Point(113.051571, 28.083055, wgs84));
        pointList.add(new Point(113.117111, 28.089173, wgs84));
        pointList.add(new Point(113.180352, 28.108547, wgs84));
        pointList.add(new Point(113.206798, 28.151362, wgs84));
        pointList.add(new Point(113.193, 28.179895, wgs84));
        pointList.add(new Point(113.189551, 28.250176, wgs84));
        pointList.add(new Point(113.190701, 28.28988, wgs84));
        pointList.add(new Point(113.167704, 28.328551, wgs84));

        //create a marker symbol
        SimpleMarkerSymbol marker = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 5);
        //create graphics
        for (Point point : pointList) {
            Graphic graphic = new Graphic(point, marker);
            //add the graphics to the graphics overlay
            graphicOverlay.getGraphics().add(graphic);
        }

    }


    private void addLine(GraphicsOverlay graphicOverlay) {
        // create a purple (0xFF800080) simple line symbol
        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF800080, 4);
        // create a new point collection for polyline
        PointCollection points01 = new PointCollection(SpatialReferences.getWgs84());
        // create and add points01 to the point collection
        //长沙1号线
        points01.add(new Point(112.9969670000, 28.2528030000));
        points01.add(new Point(112.9861150000, 28.2287460000));
        points01.add(new Point(112.9838880000, 28.2130860000));
        points01.add(new Point(112.9826660000, 28.1852620000));
        points01.add(new Point(112.9920800000, 28.1766010000));
        points01.add(new Point(112.9907870000, 28.1632910000));
        points01.add(new Point(112.9939490000, 28.1216280000));
        points01.add(new Point(112.9970390000, 28.0728740000));
        points01.add(new Point(112.9984760000, 28.0690490000));
        // create the polyline from the point collection
        Polyline polyline = new Polyline(points01);
        // create the graphic with polyline and symbol
        Graphic graphic = new Graphic(polyline, lineSymbol);

        PointCollection points02 = new PointCollection(SpatialReferences.getWgs84());

        points02.add(new Point(113.0898400000, 28.1537310000));
        points02.add(new Point(113.0609500000, 28.1515650000));
        points02.add(new Point(113.0546260000, 28.1555140000));
        points02.add(new Point(113.0511770000, 28.1620110000));
        points02.add(new Point(113.0508890000, 28.1760220000));
        points02.add(new Point(113.0454280000, 28.1821360000));
        points02.add(new Point(113.0446370000, 28.1969720000));
        points02.add(new Point(113.0186940000, 28.1987550000));
        points02.add(new Point(112.9606280000, 28.2014290000));
        points02.add(new Point(112.9490570000, 28.2076690000));
        points02.add(new Point(112.9388530000, 28.2097060000));
        points02.add(new Point(112.9314510000, 28.2133980000));
        points02.add(new Point(112.9144190000, 28.2116800000));
        points02.add(new Point(112.9149220000, 28.2049950000));
        points02.add(new Point(112.9022740000, 28.1994560000));
        points02.add(new Point(112.8884040000, 28.1918790000));

        SimpleLineSymbol lineSymbol2 = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF47f113, 4);
        Polyline polyline2 = new Polyline(points02);
        Graphic graphic2 = new Graphic(polyline2, lineSymbol2);

        // add graphic to the graphics overlay
        graphicOverlay.getGraphics().add(graphic);
        graphicOverlay.getGraphics().add(graphic2);
    }


    private void addPolygon(GraphicsOverlay graphicOverlay) {
        //边线
        SimpleLineSymbol outlineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF005000, 1);
        //填充
        SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF005000,
                outlineSymbol);
        // 点集合
        PointCollection points = new PointCollection(SpatialReferences.getWgs84());
        //
        points.add(new Point(113.1705750000, 28.0411450000));
        points.add(new Point(113.1698840000, 28.0403360000));
        points.add(new Point(113.1704280000, 28.0402610000));
        // create the polyline from the point collection
        Polygon polygon = new Polygon(points);
        // create the graphic with polyline and symbol
        Graphic graphic = new Graphic(polygon, fillSymbol);
        // add graphic to the graphics overlay
        graphicOverlay.getGraphics().add(graphic);
    }


    private void addText(GraphicsOverlay graphicOverlay) {
        // create two text symbols
        TextSymbol textSymbol = new TextSymbol(10, "Hello .", Color.BLUE, TextSymbol.HorizontalAlignment.LEFT,
                TextSymbol.VerticalAlignment.BOTTOM);
        // create points
        Point p = new Point(113.1696460000, 28.0409220000, SpatialReferences.getWgs84());
        // create graphics from the points and symbols
        Graphic graphic = new Graphic(p, textSymbol);
        // add graphics to the graphics overlay
        graphicOverlay.getGraphics().add(graphic);
    }

}
